package com.ruoyi.factorydata.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.factorydata.domain.MaterialRequisition;
import com.ruoyi.factorydata.domain.dto.MaterialRequisitionCodePrintDTO;
import com.ruoyi.factorydata.service.IMaterialRequisitionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 领料单Controller
 *
 * @author ruoyi
 * @date 2025-07-29
 */
@RestController
@RequestMapping("/materialRequisition/materialRequisitionInfo")
public class MaterialRequisitionController extends BaseController {
    @Autowired
    private IMaterialRequisitionService materialRequisitionService;

    /**
     * 查询领料单列表
     */
    @PreAuthorize("@ss.hasPermi('materialRequisition:materialRequisitionInfo:list')")
    @GetMapping("/list")
    public TableDataInfo list(MaterialRequisition materialRequisition) {
        startPage();
        List<MaterialRequisition> list = materialRequisitionService.selectMaterialRequisitionList(materialRequisition);
        return getDataTable(list);
    }

    /**
     * 导出领料单列表
     */
    @PreAuthorize("@ss.hasPermi('materialRequisition:materialRequisitionInfo:export')")
    @Log(title = "领料单", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, MaterialRequisition materialRequisition) {
        List<MaterialRequisition> list = materialRequisitionService.selectMaterialRequisitionList(materialRequisition);
        ExcelUtil<MaterialRequisition> util = new ExcelUtil<MaterialRequisition>(MaterialRequisition.class);
        util.exportExcel(response, list, "领料单数据");
    }

    /**
     * 获取领料单详细信息
     */
    @PreAuthorize("@ss.hasPermi('materialRequisition:materialRequisitionInfo:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(materialRequisitionService.selectMaterialRequisitionById(id));
    }

    /**
     * 新增领料单
     */
    @PreAuthorize("@ss.hasPermi('materialRequisition:materialRequisitionInfo:add')")
    @Log(title = "领料单", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody MaterialRequisition materialRequisition) {
        return toAjax(materialRequisitionService.insertMaterialRequisition(materialRequisition));
    }

    /**
     * 修改领料单
     */
    @PreAuthorize("@ss.hasPermi('materialRequisition:materialRequisitionInfo:edit')")
    @Log(title = "领料单", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody MaterialRequisition materialRequisition) {
        return toAjax(materialRequisitionService.updateMaterialRequisition(materialRequisition));
    }

    /**
     * 拒绝领料单
     */
    @PreAuthorize("@ss.hasPermi('materialRequisition:materialRequisitionInfo:process')")
    @Log(title = "领料单", businessType = BusinessType.UPDATE)
    @PutMapping("refuse")
    public AjaxResult refuseRequest(@RequestBody MaterialRequisition materialRequisition) {
        return toAjax(materialRequisitionService.refuseRequest(materialRequisition));
    }
    /**
     * 审核领料单
     */
    @PreAuthorize("@ss.hasPermi('materialRequisition:materialRequisitionInfo:process')")
    @Log(title = "领料单", businessType = BusinessType.UPDATE)
    @PutMapping("auth")
    public AjaxResult authRequest(@RequestBody MaterialRequisition materialRequisition) {
        return toAjax(materialRequisitionService.authRequest(materialRequisition));
    }
    /**
     * 退料
     */
    @PreAuthorize("@ss.hasPermi('materialRequisition:materialRequisitionInfo:process')")
    @Log(title = "领料单", businessType = BusinessType.UPDATE)
    @PutMapping("return")
    public AjaxResult returnRequest(@RequestBody MaterialRequisition materialRequisition) {
        return toAjax(materialRequisitionService.returnRequest(materialRequisition));
    }

    /**
     * 退料拒绝
     */
    @PreAuthorize("@ss.hasPermi('workOrder:workOrderInfo:applyMaterial')")
    @Log(title = "退料", businessType = BusinessType.UPDATE)
    @PutMapping("/returnRefuse")
    public AjaxResult returnRefuseMaterial(@RequestBody MaterialRequisition materialRequisition) {
        return toAjax(materialRequisitionService.returnRefuseMaterial(materialRequisition));
    }

    /**
     * 领料单打印
     */
    @PreAuthorize("@ss.hasPermi('materialRequisition:materialRequisitionInfo:print')")
    @Log(title = "领料单", businessType = BusinessType.UPDATE)
    @GetMapping("/print")
    public MaterialRequisitionCodePrintDTO print(MaterialRequisition materialRequisition) {
        return materialRequisitionService.print(materialRequisition);
    }
    /**
     * 删除领料单
     */
    @PreAuthorize("@ss.hasPermi('materialRequisition:materialRequisitionInfo:remove')")
    @Log(title = "领料单", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(materialRequisitionService.deleteMaterialRequisitionByIds(ids));
    }
}
